#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <ctfmod/ctfmod_core>

new Handle:ReturnTimer[DMAXOBJECTS]		= INVALID_HANDLE;
new Handle:RoundTime					= INVALID_HANDLE;
new bool:CanBuy[MAXPLAYERS+1]			= {false, ...};

public OnPluginStart()
{
	if(!LibraryExists("ctfmod_core")) SetFailState("Can't find CTFmod Core Plugin");
	
	CTFHookObjectCapture(CTFHookObjectCaptureCB);
	CTFHookObjectTake(OnObjectTake);
	CTFHookObjectDrop(OnObjectDrop);
	RemoveBuyZone();
	HookEvent("player_spawn", Event_PlayerSpawn);
	RoundTime = FindConVar("mp_roundtime");
	SetConVarBounds(RoundTime, ConVarBound_Upper , true, 30.0);
	SetConVarFloat(RoundTime, 30.0);
	
}

public OnCTFTurnOn()
{
	PrintToChatAll("TestOn");
}

public OnCTFTurnOff()
{
	PrintToChatAll("TestOff");
}

public Action:OnObjectTake(&client, &TakenObj)
{
	PrintToChatAll("Object %d Taken by %d",TakenObj, client);
	return Plugin_Continue;
}

public Action:OnObjectDrop(&DropedObj, &client)
{
	PrintToChatAll("Obj %d dropped by %d", DropedObj,client);
	return Plugin_Continue;
}
public Action:CTFHookObjectCaptureCB(&client, &CapturedObj, &CapturingObj)
{
	PrintToChatAll("%d %d %d", client, CapturedObj, CapturingObj);
	return Plugin_Continue;
}

public OnMapStart()
{
	RemoveBuyZone();
}

RemoveBuyZone()
{
	new entCount = GetMaxEntities();
	decl String:entName[64];
	
	for(new i=0; i<entCount; i++) {
		if(!IsValidEntity(i))
			continue;
		
		GetEdictClassname(i, entName, 63);
		
		if(!StrEqual(entName, "func_buyzone"))
			continue;
		
		RemoveEdict(i); 
	}
}
public Action:Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	CanBuy[client] = true;
	CreateTimer(15.0, TActivateBuy, client);
}

public Action:TActivateBuy(Handle:timer, any:client)
{
	CanBuy[client] = false;
}
/*
public OnGameFrame()
{
	decl i;
	for(i = 1; i <= MaxClients; i++)
	{
		if(CanBuy[i]==true) SetEntProp(i, Prop_Send, "m_bInBuyZone", 1);
	}
}
*/
//SetEntProp(client, Prop_Send, "m_bInBuyZone", 1);